package waal.pc.frontend.tiles;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import waal.pc.backend.channels.AnalogOutputChannel;
import waal.pc.backend.channels.Channel;

public class AnalogOutputTile extends ChannelTile implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JTextField field;
	private AnalogOutputChannel ch;
	private JButton sendButton;
	
	public AnalogOutputTile() {
		super("DAC");
		
	}

	@Override
	public ChannelTile getTile(Channel channel) {
		this.initTile(channel);
		this.ch =  (AnalogOutputChannel) channel;
		this.add(new JLabel(ch.toString() + ": "));
		this.field = new JTextField(20);
		this.sendButton = new JButton("Send");
		this.sendButton.addActionListener(this);
		this.add(field);
		this.add(this.sendButton);
		if(channel.isActive()) {
			this.sendButton.setEnabled(true);
			
		} else {
			this.sendButton.setEnabled(false);
		}
		return this;
	}

	@Override
	public ChannelTile clone() {
		return new AnalogOutputTile();
	}

	@Override
	public boolean isDigital() {
		return false;
	}

	@Override
	public boolean isInput() {
		return false;
	}

	@Override
	public boolean isOutput() {
		return true;
	}

	@Override
	public boolean isUpdateAble() {
		return false;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		double value = Double.valueOf(this.field.getText());
		if(value != Double.NaN) {
			this.ch.sendOutput(value);
		}
	}

	@Override
	public void closeTile() {
		this.ch = null;
		
	}

	@Override
	protected void hubStart() {
		this.sendButton.setEnabled(true);
	}

	@Override
	protected void hubStop() {
		this.sendButton.setEnabled(false);
		
	}

	@Override
	public void setDimension(int width, int heigh) {
		// TODO Auto-generated method stub
		
	}

	
}
